package mo.offer_special;

import java.util.Stack;

public class L037 {

    public int[] asteroidCollision(int[] asteroids) {
        Stack<Integer> stack = new Stack<>();
        int p = 0;
        while (p < asteroids.length) {
            if (stack.isEmpty() || asteroids[p] > 0 || stack.peek() < 0) {
                stack.add(asteroids[p]);
            } else {
                if (stack.peek() <= -asteroids[p]) {
                    if (stack.pop() < -asteroids[p]) {
                        continue;
                    }
                }
            }
            p++;
        }
        return stack.stream().mapToInt(Integer::intValue).toArray();
    }

}
